package supimpa.spacegame.src;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class MainClass {

	public static void main(String[] args){
		
		new MainClass().run();
	}
	
	Camera camera = new Camera();
	
	public void run(){
		
		try{
			
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.create();
		}catch(LWJGLException e){
			
			e.printStackTrace();
			System.exit(0);
		}
		
		initGL3D();
		getDelta();
		lastFPS = getTime();
		Mouse.setGrabbed(true);
		
		while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
			
			update();
			render();
			Display.update();
		}
		
		Display.destroy();
	}
	
	private void update(){
		
		camera.updateCamera();
		updateFPS();
	}
	
	private void render(){
		
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		
		camera.transformWorld();
	}
	
	
	
	
	
	
	private void initGL3D(){
		
		glClearColor(0.9f, 0.9f, 1.0f, 0.0f);
		glMatrixMode(GL11.GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f,((float)Display.getWidth())/((float)Display.getHeight()),0.1f,100.0f);
        glMatrixMode(GL11.GL_MODELVIEW);
        glLoadIdentity();
        glEnable(GL11.GL_TEXTURE_2D);
        glShadeModel(GL11.GL_SMOOTH);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClearDepth(1.0f);
        glEnable(GL11.GL_DEPTH_TEST);
        glDepthFunc(GL11.GL_LEQUAL);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	}
	
	public void initGL2D(){
		
		glMatrixMode(GL_PROJECTION);
	    glLoadIdentity();
	    glOrtho(0, 800, 0, 600, 1, -1);
	    glMatrixMode(GL_MODELVIEW);
	    glLoadIdentity();
	    glTranslatef(0.375f, 0.375f, 0.0f);
	    glDisable(GL_DEPTH_TEST);
	}
	
	long lastFrame;
	int fps;
	long lastFPS;
	public int getDelta(){
	    long time = getTime();
	    int delta = (int) (time - lastFrame);
	    lastFrame = time;	 
	    return delta;
	}
	public long getTime() {
	    return (Sys.getTime() * 1000) / Sys.getTimerResolution();
	}
	public void updateFPS() {
		if (getTime() - lastFPS > 1000) {
			Display.setTitle("FPS: "+fps);
			fps = 0;
			lastFPS += 1000;			
		}
		fps++;
	}
}
